package com.bt.lms.service.impl;

import com.bt.lms.constant.BOConstant;
import com.bt.lms.dao.servise.RightDao;
import com.bt.lms.dao.servise.impl.RightDaoImpl;
import com.bt.lms.service.RightService;
import com.bt.lms.service.RoleService;

import java.util.List;

/**
 * 权限服务
 *
 * @author 徐大永
 */
public class RightServiceImpl implements RightService {

    @Override
    public List<Integer> roelRight(Long id) {

        //角色权限中间表
        StringBuilder sqlSB = new StringBuilder();
        sqlSB.append("SELECT right_id FROM rl_role_right WHERE is_delete=").append(BOConstant.DELETED_FALSE).append(" and role_id=?");
        String sql = new String(sqlSB);

        //dao 方法
        RightDao rightDao = new RightDaoImpl();
        return rightDao.select(sql, id);
    }

    @Override
    public boolean roleSetRight(Long id, List<Integer> rights) {

        //角色权限中间表
        String sql = "INSERT INTO rl_role_right (role_id,right_id) VALUES (?,?)";

        //dao 方法
        RightDao rightDao = new RightDaoImpl();
        int i = rightDao.inserts(sql, id, rights);

        int size = rights.size();
        return i == size;


    }

    @Override
    public List<Integer> userRight(Long id) {

        //用户角色中间表 + 角色权限中间表
        //用户角色中间表
        RoleService roleService = new RoleServiceImpl();
        List<Long> roleIds = roleService.queryRoleFromUser(id);

        //查询所有角色的所有权限(批量执行
        StringBuilder sqlSB = new StringBuilder();
        sqlSB.append("SELECT right_id FROM rl_role_right WHERE is_delete=").append(BOConstant.DELETED_FALSE).append(" and role_id=?");
        String sql = new String(sqlSB);
        //dao 对象（权限）
        RightDao rightDao = new RightDaoImpl();
        return rightDao.selects(sql,roleIds);
    }
}
